package com.zengdada.authority.mapper;

import com.zengdada.authority.model.AppPower;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface AppPowerMapper extends Mapper<AppPower> {

    @Select("select p.url, r.* from syoauth.app_power p, syoauth.app_power_role_rel prr, syoauth.app_role r " +
            "where p.id = prr.app_power_id(+)  and prr.app_role_id = r.id and p.url is not null and  p.state <> 0 AND p.intercept <> 0  and p.application_id in ('0',#{applicationId,jdbcType=VARCHAR}) and r.application_id in ('0',#{applicationId,jdbcType=VARCHAR})" +
            "and r.type = #{type,jdbcType=DECIMAL}")
    List<Map<String, Object>> selectAllResource(@Param("applicationId") String appId, @Param("type") String type);

    @Select("<script> select nn.*, level leaf from ( " +
            "        select distinct * from ( " +
            "          <if test=\"userId != null\"> select p.id,p.parent_id,p.name title,p.url href,p.img icon,p.css style,p.state,p.sort from " +
            "              <if test=\"isSuperM != true\"> syoauth.app_user_role_rel ur, syoauth.app_role r, syoauth.app_power_role_rel pr,</if> " +
            "              syoauth.app_power p where p.application_id in ('0',#{applicationId,jdbcType=VARCHAR}) and p.category = 2 and p.intercept = 1 " +
            "              <if test=\"isSuperM != true\"> " +
            "                  and ur.app_role_id = r.id and pr.app_role_id = r.id and p.id = pr.app_power_id and ur.app_user_id = #{userId,jdbcType=VARCHAR} " +
            "                  and r.application_id in ('0',#{applicationId,jdbcType=VARCHAR}) and r.state = 1 " +
            "              </if> " +
            "           union all " +
            "         </if> " +
            "        select p.id,p.parent_id,p.name title,p.url href,p.img icon,p.css style,p.state,p.sort from syoauth.app_power p where p.state = 1 and p.intercept = 0 and p.category = 2 and p.application_id in ('0',#{applicationId,jdbcType=VARCHAR}) " +
            "    )) nn start with nn.parent_id is null connect by nn.parent_id = prior nn.id order by level, sort </script>")
    List<Map<String, Object>> selectNavMenu(@Param("userId") String userId, @Param("isSuperM") boolean isSuperAdministrator, @Param("applicationId") String applicationId);

    @Select("<script>select p.id, p.name, p.parent_id,(case when pid is null then 0 else 1 end) \"checked\" from " +
            "        <if test='roleId == \"1111111111111111111111111111111\" and isSuperM == true'> syoauth.app_power </if> " +
            "        <if test='roleId != \"1111111111111111111111111111111\" or isSuperM != true'> " +
            "            (select ap.* from syoauth.app_power_role_rel rr, syoauth.app_power ap where rr.state = 1 and ap.id = rr.app_power_id " +
            "            and rr.app_role_id = (select (case when  aur.parent_id is null then aur.id else aur.parent_id end) id from syoauth.app_role aur where aur.id = #{roleId,jdbcType=VARCHAR})) " +
            "        </if> p, " +
            "        (select app_power_id pid from syoauth.app_power_role_rel where state = 1 and app_role_id = #{roleId,jdbcType=VARCHAR}) prr  " +
            "        where p.id = prr.pid(+) and p.application_id in ('0',#{applicationId,jdbcType=VARCHAR}) ORDER BY (select count(0) from syoauth.app_power t where t.parent_id = p.id),p.name </script>")
    List<Map<String, Object>> selectPowerByRole(@Param("roleId") String roleId, @Param("isSuperM") boolean isSuperAdministrator, @Param("applicationId") String applicationId);
}